//
//  FYListViewController.m
//  ThePainter
//
//  Created by Sean on 15/8/9.
//  Copyright (c) 2015年 FeiYu. All rights reserved.
//

#import "FYFirstListPage.h"

#import "FYFirstListCell.h"
#import "FYProductDetailPage.h"
#import "FYFirstListViewModel.h"
#import "FYFirstListModel.h"
#import "FYUserPage.h"

@interface FYFirstListPage () <FYFirstListCellDelegate>

@property (nonatomic) CGFloat cellHeight;
@property (nonatomic) CGFloat cellViewW;
@property (nonatomic) CGFloat cellViewH;
@property (nonatomic) FYFirstListViewModel *viewModel;
@property (nonatomic) NSArray *products;

@end

@implementation FYFirstListPage

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"画家";
    
    self.cellHeight = [FYUtils fyScreenWidth] + [FYUtils fyScreenWidth] / 3;
    self.cellViewW = [FYUtils fyScreenWidth];
    self.cellViewH = (CGFloat)self.cellHeight;
    
    self.viewModel = [[FYFirstListViewModel alloc] init];
    
    //加载数据
    [self loadNewData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.products.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.cellHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    FYFirstListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[FYFirstListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell loadModel:self.products[indexPath.row]];
    }
    [cell setDelegate:self];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //跳转到详情页面
    FYFirstListModel *product = self.products[indexPath.row];
    FYProductDetailPage *page = [[FYProductDetailPage alloc] initWithProductId:product.product.productId];
    //隐藏tabbar
    page.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:page animated:YES];
}

- (void)didSelectAvatar:(NSString *)userId {
    FYUserPage *page = [[FYUserPage alloc] initWithUserId:userId];
    //隐藏tabbar
    page.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:page animated:YES];
}

#pragma mark - get data

- (void)loadNewData {
    
    [self.viewModel loadNewData:self.products valueBlock:^(id returnValue) {
        self.products = returnValue;
        [self.tableView reloadData];
        [self finishLoadNewData];
        [self resetNoMore];
    } errorBlock:^(id errorCode) {
        [self finishLoadNewData];
    } failureBlock:^{
        [self finishLoadNewData];
    }];
}

- (void)loadMoreData {

    [self.viewModel loadMoreData:self.products valueBlock:^(id returnValue) {
        self.products = returnValue;
        [self.tableView reloadData];
        [self finishLoadMoreData];
    } errorBlock:^(id errorCode) {
        
    } failureBlock:^ {
        //提示没有更多数据
        [self noMoreData];
    }];
}

@end
